
package cz.czu.pef.DataModelling;

import java.util.*;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cz.czu.pef.DataModelling.utils.Lazy;

@Entity
@Table(name = "MEDICAL_EXAMINATION_TYPES")
@NamedQueries({
	@NamedQuery(name="findMedicalExaminationTypeByCode", query="SELECT met FROM MedicalExaminationType met WHERE met.code = :code")
})
@ManagedBean
@ViewScoped
@Component
public class MedicalExaminationType extends CommonPersistenceObject
{
	private static final long serialVersionUID = -4318181001409286326L;

	@OneToMany(fetch=FetchType.LAZY, targetEntity=MedicalExaminationTypeForRoleType.class, mappedBy="medicalExaminationType") // no cascade!
	private List<MedicalExaminationTypeForRoleType> medicalExaminationTypeForRoleTypes;

	@OneToMany(fetch=FetchType.LAZY, targetEntity=MedicalExamination.class, mappedBy="medicalExaminationType") // no cascade!
	//@OnDelete(action=OnDeleteAction.NO_ACTION)
	private Set<MedicalExamination> medicalExaminations;

	private MedicalExaminationType()
	{
		super(null, null, null);
	}

	public MedicalExaminationType(String medicalExaminationTypeCode, String medicalExaminationTypeName, String medicalExaminationTypeDescription)
	{
		super(medicalExaminationTypeCode, medicalExaminationTypeName, medicalExaminationTypeDescription);
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public Set<MedicalExamination> getMedicalExaminations()
	{
		return medicalExaminations;
	}

	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	@Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("MedicalExaminationType [toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}

	
	@Lazy
	@Transactional(readOnly=true, propagation=Propagation.REQUIRED)
	public List<MedicalExaminationTypeForRoleType> getMedicalExaminationTypeForRoleTypes()
	{
		return medicalExaminationTypeForRoleTypes;
	}
	
	/**
	 * Can be deleted after removing related entities (no cascade).
	 */
	@Override
	public boolean canBeRemoved()
	{
		return getMedicalExaminationTypeForRoleTypes().size() == 0 && getMedicalExaminations().size() == 0;
	}
}